/**
 * 北京百年育人教育投资有限公司 版权所有
 * <p>
 * 文件:com.example.test.VolleyUtils.java
 * <p>
 * 创建时间:2015年5月6日
 * <p>
 * 创建人:马海明
 */
package com.sxtyxt.volley;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.sxtyxt.online.App;
import com.sxtyxt.util.NetUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 网络请求工具类
 *
 * @author 马海明
 * @created 马海明 2015年5月6日
 */
public class VolleyUtils {

    /**
     * 请求接口
     *
     * @param url      请求url
     * @param listener 结果回调
     */
    public static void requestService(String url, final Map<String, String> params, final ResultLinstener listener) {

        //无网络不进行请求
        if (!NetUtils.isNetworkConnected()) {
            listener.onIOError();
            listener.onError();
            return;
        }

        listener.onSetTag(url);
        System.out.println(url);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println("onResponse");
                listener.onSuccess(response);
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                listener.onError();
                if (error instanceof NetworkError) {
                    listener.onIOError();
                } else if (error instanceof ServerError) {
                    listener.onServerError();
                } else if (error instanceof NoConnectionError) {
                    listener.onNoConnectionError();
                } else if (error instanceof TimeoutError) {
                    listener.onTimeOutError();
                }
            }
        }) {

            @Override
            protected String getParamsEncoding() {
                return "UTF-8";
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }

        };
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(60 * 1000, 0, 1));
        // Add the request to the RequestQueue.
        App.addRequest(stringRequest, url);

    }

    /**
     * 带文件post请求
     *
     * @param url          请求url
     * @param params       其他请求参数
     * @param filePartName 文件参数名
     * @param files        文件List
     * @param listener     结果回调
     */
    public static void requestServiceWithFile(String url, final Map<String, String> params
            , String filePartName, List<File> files, final ResultLinstener listener) {
        //无网络不进行请求
        if (!NetUtils.isNetworkConnected()) {
            listener.onIOError();
            listener.onError();
            return;
        }

        listener.onSetTag(url);
        System.out.println(url);

        MultipartRequest multipartRequest = new MultipartRequest(url, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                listener.onError();
                if (error instanceof NetworkError) {
                    listener.onIOError();
                } else if (error instanceof ServerError) {
                    listener.onServerError();
                } else if (error instanceof NoConnectionError) {
                    listener.onNoConnectionError();
                } else if (error instanceof TimeoutError) {
                    listener.onTimeOutError();
                }
            }
        }, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println("onResponse");
                listener.onSuccess(response);
            }
        }, filePartName, files, params) {
            @Override
            protected String getParamsEncoding() {
                return "UTF-8";
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }

            @Override
            public String getBodyContentType() {
                return super.getBodyContentType();
            }
        };


        multipartRequest.setRetryPolicy(new DefaultRetryPolicy(3 * 60 * 1000, 0, 1));
        // Add the request to the RequestQueue.
        App.addRequest(multipartRequest, url);

    }



    /**
     * 带文件post请求
     *
     * @param url          请求url
     * @param params       其他请求参数
     * @param filePartNames 文件参数名
     * @param files        文件List
     * @param listener     结果回调
     */
    public static void requestServiceWithFile(String url, final Map<String, String> params
            , List<String> filePartNames, List<File> files, final ResultLinstener listener) {
        //无网络不进行请求
        if (!NetUtils.isNetworkConnected()) {
            listener.onIOError();
            listener.onError();
            return;
        }

        listener.onSetTag(url);
        System.out.println(url);

        MultipartRequest multipartRequest = new MultipartRequest(url, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                listener.onError();
                if (error instanceof NetworkError) {
                    listener.onIOError();
                } else if (error instanceof ServerError) {
                    listener.onServerError();
                } else if (error instanceof NoConnectionError) {
                    listener.onNoConnectionError();
                } else if (error instanceof TimeoutError) {
                    listener.onTimeOutError();
                }
            }
        }, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println("onResponse");
                listener.onSuccess(response);
            }
        }, filePartNames, files, params) {
            @Override
            protected String getParamsEncoding() {
                return "UTF-8";
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
        };


        multipartRequest.setRetryPolicy(new DefaultRetryPolicy(3 * 60 * 1000, 0, 1));
        // Add the request to the RequestQueue.
        App.addRequest(multipartRequest, url);

    }





    /**
     * 取消请求
     *
     * @param tag
     */
    public static void cancelService(String tag) {
        App.getRequestQueue().cancelAll(tag);
    }

    /**
     * 取消请求
     */
    public static void cancelService() {
        App.getRequestQueue().cancelAll(new RequestQueue.RequestFilter() {
            @Override
            public boolean apply(Request<?> request) {
                System.out.println("request.getUrl();" + request.getUrl());
                return true;
            }
        });
    }

}
